<template>
  <md-popup id="popup-container" v-model="value" position="bottom" :has-mask="true" prevent-scroll @hide="cancel">
    <md-popup-title-bar title="评价" ok-text cancel-text="取消" @confirm="confirm" @cancel="cancel"></md-popup-title-bar>
    <div class="taxiPayType">
      <div class="taxi_driver_box">
        <div class="taxi_driver_box_left">
          <p v-if="driverImg">
            <img :src="driverImg" />
          </p>
          <p v-else>
            <img src="@/assets/imgs/snyc_tx.png" />
          </p>
        </div>
        <div class="taxi_driver_box_center">
          <p class="taxi_driver_text">
            <span>{{driverName}}</span>
            <b>{{driverCarNumber}}</b>
            <em>
              <i>{{driverServiceNumber}}</i>单
            </em>
          </p>
          <p class="taxi_company_text">{{getDriverCompany}}</p>
        </div>
        <div class="taxi_driver_box_right">
          <p>
            <!-- <span><img src="@/assets/imgs/snyc_jjz_tel.png"></span> -->
            <span>
              <img src="@/assets/imgs/snyc_jjz_tel_no.png" />
            </span>
          </p>
        </div>
      </div>
      <div class="endOrdermoney">
        <p class="money_alt">￥{{lastOrderAmt}}</p>
        <p class="endOrdercharges">
          费用明细
          <md-icon name="wdqb-more" slot="left" class="quxiao_icon" @click="taxiCost"></md-icon>
        </p>
      </div>
      <div class="driver_evaluate">
        <p class="evaluate_hd">我对司机的评价</p>
        <div class="starWrap">
          <img :src="level>index?stara:starb" v-for="(item,index) in levelList" :key="index" @click="getlevel(index)" />
          <p v-show="IsevaluateResult">{{evaluateResulttext}}</p>
        </div>
        <div>
          <ul class="evaluationList">
            <li
              v-for="(item, index) in evaluationList"
              :key="index"
              @click="getevaluationList(index,item)"
              :class="{'evaluation-active':evaluationArr[index]}"
              ref="menuItem"
            >{{item}}</li>
          </ul>
        </div>
        <div style="clear:both"></div>
        <div class="complaints_input">
          <textarea placeholder="还想对司机说……" v-model="evaluationDescribe"></textarea>
        </div>
      </div>
      <p class="taxi_pay" @click="subTaxiEvaluate">提交评价</p>
    </div>
  </md-popup>
</template>
<script>
import '@/assets/svgs/wdqb-more.svg';
import { Popup, PopupTitleBar, Picker, Icon, Toast, Dialog } from 'mand-mobile';
import { mapGetters, mapActions, mapMutations } from 'vuex';
import { decryptByDESModeEBC, encryptByDESModeEBC } from '@/common/encrypt';
import Loading from '@/components/loading';
import http from '@/common/http';
var moment = require('moment');
moment.locale('zh-cn');
export default {
  name: 'time-selector',
  components: {
    [Popup.name]: Popup,
    [PopupTitleBar.name]: PopupTitleBar,
    [Icon.name]: Icon,
    [Picker.name]: Picker
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    appendTo: {
      default: () => document.body
    }
  },
  data() {
    return {
      driverName: '', // 司机姓名
      driverImg: '', // 司机头像
      driverCarNumber: '', // 司机车牌号
      driverServiceNumber: '', // 司机接单次数
      lastOrderAmt: '', // 订单金额
      evaluationArr: [],
      evaluationnewArr: [],
      evaluationDescribe: '',
      evaluateResulttext: '',
      IsevaluateResult: false,
      level: '0',
      levelList: [0, 1, 2, 3, 4],
      stara: require('@/assets/imgs/xcxq_icon_xing_light@2x.png'), // 亮星星
      starb: require('@/assets/imgs/xcxq_icon_xing_dark@2x.png'), // 暗星星
      evaluationList: []
    };
  },
  created() {
    let str = this.getTaxiOrderInfoData;
    if (!str) {
      return false;
    }
    this.evaluationList = str.all_evaluate_label.split(',');
    this.driverImg = this.getTaxiOrderInfoData.driver_image;
    this.driverName = this.getTaxiOrderInfoData.driver_name;
    this.driverCarNumber = this.getTaxiOrderInfoData.tx_car_plate_number;
    this.driverServiceNumber = this.getTaxiOrderInfoData.service_num;
    this.lastOrderAmt = decryptByDESModeEBC(this.getTaxiOrderInfoData.last_order_amt);
    this.putevaluation();
  },
  methods: {
    confirm() {
      this.value = false;
      this.$emit('submit');
    },
    cancel() {
      this.value = false;
    },
    // 获取评价星星
    getlevel(val) {
      if (val === 0) {
        this.evaluateResulttext = '非常不满意';
      } else if (val === 1) {
        this.evaluateResulttext = '不满意';
      } else if (val === 2) {
        this.evaluateResulttext = '一般';
      } else if (val === 3) {
        this.evaluateResulttext = '满意';
      } else if (val === 4) {
        this.evaluateResulttext = '非常满意';
      }
      this.IsevaluateResult = true;
      this.level = val + 1;
    },
    getevaluationList(index, item) {
      for (let i = 0; i < this.evaluationArr.length; i++) {
        if (i === index) {
          this.evaluationArr[index] ? this.evaluationArr.splice(index, 1, false) : this.evaluationArr.splice(index, 1, true);
          if (this.evaluationArr[index]) {
            this.evaluationnewArr.push(this.evaluationList[index]);
          } else {
            this.removeArr(item);
          }
        }
      }
      // console.log(this.evaluationnewArr);
    },
    queryIndexOf(item) {
      for (var i = 0; i < this.evaluationnewArr.length; i++) {
        if (this.evaluationnewArr[i] === item) {
          return i;
        }
      }
      return -1;
    },
    removeArr(item) {
      var index = this.queryIndexOf(item);
      if (index > -1) {
        this.evaluationnewArr.splice(index, 1);
      }
    },
    putevaluation() {
      for (let i = 0; i < this.evaluationList.length; i++) {
        this.evaluationArr.push(false);
      }
    },
    // 费用明细
    taxiCost() {
      this.value = false;
      this.$router.push({
        name: 'taxichargesDetails',
        query: {
          orderno: this.$route.query.orderno
        }
      });
    },
    // 提交评价
    subTaxiEvaluate() {
      if (this.level === '0') {
        Toast({
          content: '请选择星级评价',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      let params = {
        order_no: this.$route.query.orderno,
        evaluate_label: this.evaluationnewArr.join(','), // 评价标签
        evaluate_level: this.level.toString(), // 星级评价
        evaluate_text: this.evaluationDescribe // 评价文本
      };
      Loading.show();
      http.post('/pakj/utaxi/saveOrderEvaluate', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          Dialog.alert({
            title: '提示',
            content: '评价成功',
            confirmText: '确定',
            onConfirm: () => {
              this.$emit('submit');
            }
          });
        } else {
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定'
          });
        }
      });
    }
  },
  mounted() {
    if (this.appendTo) {
      this.appendTo.appendChild(this.$el);
    }
  },
  beforeDestroy() {
    if (this.appendTo) {
      this.appendTo.removeChild(this.$el);
    }
  },
  computed: {
    ...mapGetters(['getTaxiOrderInfoData']),
    getDriverCompany() {
      var str = this.getTaxiOrderInfoData;
      if (!str) {
        return '';
      }
      if (str.operation_company.length > 16) {
        return str.operation_company.substring(0, 16) + '...';
      } else {
        return str.operation_company;
      }
    }
  }
};
</script>

<style lang="stylus">
#popup-container {
  p {
    display: block;
    margin-block-start: 0px;
    margin-block-end: 0px;
    margin-inline-start: 0px;
    margin-inline-end: 0px;
  }

  .taxiPayType {
    background-color: #fff;
  }

  .taxi_pay {
    background-color: #ff6917;
    height: 98px;
    line-height: 98px;
    font-size: 36px;
    color: #fff;
    text-align: center;
  }

  .quxiao_icon {
    width: 24px;
    height: 24px;
    position: relative;
    top: 3px;
    margin-right: 8px;
  }

  .taxi_driver_box {
    height: 160px;
    border-bottom: solid 1px #E0E0E0;
    border-top: solid 1px #E0E0E0;
  }

  .taxi_driver_box_left {
    float: left;
    margin-left: 30px;
    margin-top: 30px;
  }

  .taxi_driver_box_left p img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
  }

  .taxi_driver_box_center {
    float: left;
    margin-top: 30px;
    margin-left: 20px;
  }

  .taxi_driver_box_center p {
    margin-top: 10px;
  }

  .taxi_driver_text span {
    font-size: 30px;
    color: #333;
  }

  .taxi_driver_text b {
    margin-left: 10px;
    background-color: #FF6917;
    font-size: 20px;
    color: #fff;
    padding: 5px 6px;
    font-weight: 400;
  }

  .taxi_driver_text em {
    font-size: 24px;
    color: #666;
    font-style: normal;
  }

  .taxi_driver_text em i {
    color: #FF5C00;
    margin: 0px 5px;
    font-style: normal;
  }

  .taxi_company_text {
    font-size: 24px;
    color: #666;
  }

  .taxi_driver_box_right {
    float: right;
    margin-top: 60px;
    margin-right: 30px;
  }

  .taxi_driver_box_right p img {
    width: 44px;
    height: 44px;
  }

  .taxi_driver_box_right span {
    margin-left: 10px;
  }

  .orderinformation_bottom {
    padding: 0px 30px;
  }

  .endOrdermoney {
    text-align: center;
    padding-top: 30px;
    padding-bottom: 40px;
  }

  .money_alt {
    font-size: 40px;
    color: #333;
  }

  .endOrdercharges {
    font-size: 24px;
    color: #999;
    margin-top: 10px;
  }

  .starWrap {
    width: 100%;
    text-align: center;
    margin-top: 30px;
  }

  .starWrap img {
    width: 68px;
    height: 62px;
    margin-left: 20px;
  }

  .starWrap p {
    font-size: 26px;
    color: #666;
    margin-top: 10px;
  }

  .driver_evaluate {
    position: relative;
    border-top: solid 1px #E0E0E0;
    margin: 0px 30px;
  }

  .evaluate_hd {
    background-color: #fff;
    font-size: 28px;
    color: #666;
    position: absolute;
    left: 50%;
    margin-left: -108px;
    top: -22px;
  }

  .evaluationList li {
    height: 72px;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    float: left;
    text-align: center;
    line-height: 72px;
    margin-left: 5%;
    margin-top: 20px;
    font-size: 28px;
    color: #666;
    padding: 0px 25px;
  }

  .evaluationList li.evaluation-active {
    border: 1px solid #ff6917;
    color: #ff6917;
  }

  .complaints_input {
    background-color: #fff;
    margin-top: 20px;
    padding: 0px 30px 20px 30px;
  }

  .complaints_input textarea {
    border: none;
    resize: none;
    width: 95%;
    height: 120px;
    background-color: #F8F8F8;
    padding: 10px 20px;
  }

  .slider {
    width: 200vw;
    transform: translate3d(0, 0, 0);
    transition: transform 0.5s ease-in-out;

    .container {
      background-color: #fff;
      height: 432px;
      width: 100vw;
      float: left;
      font-size: 22px;

      .hint {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        height: 88px;
        font-size: 22px;
        color: color-text-caption;

        .hint-icon {
          width: 26px;
          height: 24px;
        }

        .theme-str {
          color: theme-color;
        }
      }

      .flex-box {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 88px;

        .waiting-hint {
          display: -webkit-box;
          font-size: 22px;

          .hint-icon {
            width: 26px;
            height: 24px;
          }

          .hint-str {
            color: theme-color;
          }

          .waiting-hint {
            color: color-text-caption;
            max-width: 25em;
            text-align: center;
          }
        }
      }
    }
  }
}
</style>
